package com.dim.list_orders;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class orders extends Activity {
	private SQLiteDatabase database;
	private ListView listView;
	private Spinner s_categoryes;
	private Cursor cursor;
	private Cursor cursor_cat;
	private SimpleCursorAdapter scAdapter;
	private SimpleCursorAdapter sc_categoryes;
	private int id_cat = 1;
	SharedPreferences prefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(com.dim.list_orders.R.layout.orders);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				com.dim.list_orders.R.layout.title_add);
		TextView title = (TextView) findViewById(com.dim.list_orders.R.id.title);
		title.setText(com.dim.list_orders.R.string.l_orders);

		listView = (ListView) findViewById(R.id.list);
		s_categoryes = (Spinner) findViewById(com.dim.list_orders.R.id.s_categoryes);
		class_sqlite dbOpenHelper = new class_sqlite(this,
				getString(com.dim.list_orders.R.string.db_name));
		database = dbOpenHelper.openDataBase();
		load_categoryes();
		get_cursor_all_orders();

		scAdapter = new SimpleCursorAdapter(this,
				com.dim.list_orders.R.layout.row_list, cursor, new String[] {
						"name", "count(*)" }, new int[] { R.id.text1,
						R.id.text2 });
		listView.setAdapter(scAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent i = new Intent(getBaseContext(), orders_details.class);
				i.putExtra("id_u", cursor.getInt(cursor.getColumnIndex("id_u")));
				i.putExtra("id_cat",
						cursor_cat.getInt(cursor_cat.getColumnIndex("_id")));
				// i.putExtra("user",
				// cursor.getString(cursor.getColumnIndex("name")));
				// i.putExtra("category",
				// cursor_cat.getString(cursor_cat.getColumnIndex("name")));
				startActivity(i);
			}
		});
		
		//font_size();

		s_categoryes.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				reload();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	void font_size() {
		TextView t1 = (TextView) findViewById(R.id.text1);
		TextView t2 = (TextView) findViewById(R.id.text2);
				String str = prefs.getString("font_size", "2");
		if (str.equals("1")) {
			t1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));
			t2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_small));

		} else if (str.equals("3")) {
			t1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
			t2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_large));
		} else {
			t1.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));
			t2.setTextSize(getResources().getDimension(
					com.dim.list_orders.R.dimen.text_medium));

		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, 0, Menu.NONE, com.dim.list_orders.R.string.m_add)
				.setIcon(android.R.drawable.ic_menu_add);
		menu.add(Menu.NONE, 3, Menu.NONE,
				com.dim.list_orders.R.string.m_orders_list).setIcon(
				android.R.drawable.ic_menu_more);
		menu.add(Menu.NONE, 2, Menu.NONE, com.dim.list_orders.R.string.m_filtr)
				.setIcon(android.R.drawable.ic_menu_crop);
		menu.add(Menu.NONE, 1, Menu.NONE,
				com.dim.list_orders.R.string.m_refresh).setIcon(
				android.R.drawable.ic_menu_revert);
		menu.add(Menu.NONE, 4, Menu.NONE,
				com.dim.list_orders.R.string.m_preference).setIcon(
				android.R.drawable.ic_menu_preferences);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			b_add(listView);
			break;
		case 1:
			reload();
			break;
		case 2:
			Intent i = new Intent(this, filtr.class);
			i.putExtra("table", "status");
			startActivity(i);
			break;
		case 3:
			Intent a = new Intent(this, orders_list_items.class);
			a.putExtra("id_cat",
					cursor_cat.getInt(cursor_cat.getColumnIndex("_id")));
			a.putExtra("name",
					cursor_cat.getString(cursor_cat.getColumnIndex("name")));
			startActivity(a);
			break;
		case 4:
			Intent c = new Intent(this, preferences.class);
			startActivity(c);
			break;
		default:
			break;
		}

		return false;
	}

	private void load_categoryes() {
		stopManagingCursor(cursor_cat);
		cursor_cat = database.query("categories",
				new String[] { "_id", "name" }, null, null, null, null, "name");
		startManagingCursor(cursor_cat);
		sc_categoryes = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, cursor_cat,
				new String[] { "name" }, new int[] { android.R.id.text1 });
		sc_categoryes
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s_categoryes.setAdapter(sc_categoryes);
	}

	private void get_cursor_all_orders() {
		stopManagingCursor(cursor);
		id_cat = cursor_cat.getInt(cursor_cat.getColumnIndex("_id"));
		cursor = database
				.rawQuery(
						"SELECT orders._id, users.name, id_u, count(*) from orders left join users on orders.id_u=users._id left join status on orders.id_st=status._id where id_cat='"
								+ id_cat
								+ "' and status.show=1 group by orders.id_u order by users.name asc",
						null);
		startManagingCursor(cursor);
	}

	private void reload() {
		get_cursor_all_orders();
		scAdapter.changeCursor(cursor);
	}

	public void b_add(View v) {
		Intent i = new Intent(getBaseContext(), orders_manager.class);
		i.putExtra("id_cat", id_cat);
		startActivity(i);
	}

	protected void onDestroy() {
		super.onDestroy();
		database.close();
	}
}